package arithmetic.array;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 数组去重
 */
public class DuplicateRemoval {

    public static void main(String[] args) {
        int[] arr = {1, 3, 3, 4, 5};
        useSet(arr);
        //useList(arr);
    }

    public static void useList(int[] arr) {
        List<Integer> list = new ArrayList();
        for (int i = 0; i < arr.length; i++) {
            if (!list.contains(arr[i])) {
                list.add(arr[i]);
            }
        }
        //toArray()方法会返回一个包含集合所有元素的Object类型数组
        Object[] newArr = list.toArray();
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(" " + newArr[i]);
        }
    }

    public static void useSet(int[] arr) {
        Set set = new HashSet();
        for (int i = 0; i < arr.length; i++) {
            set.add(arr[i]);
        }
        Object[] newArr = set.toArray();
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(" " + newArr[i]);
        }
    }
}
